﻿Imports Microsoft.Office.Interop.Word

Public Class MyDocument

    Public Sub New()
        'bis jetzt nichts
    End Sub

    Public Sub New(ByVal numberOfItems As Integer)
        Me.New()
        ReDim paragraphs(numberOfItems - 1)
        ReDim globalFormats(0)
    End Sub

    Public Function indexByFormat(ByVal format As MyFormat) As Integer
        For i As Integer = 0 To (globalFormats.Length - 1)
            If globalFormats(i).isEqual(format) Then
                Return i
            End If
        Next
        Return -1
    End Function

    Public Sub insertPicture(ByVal pic As MyPicture)

        ' falls leeres Array
        If pictures Is Nothing Then
            picture_pushBack(pic)
            Return
        End If

        For i As Integer = 0 To pictures.Length - 1
            If pic.page < pictures(i).page Or (pic.page = pictures(i).page And pic.toTop < pictures(i).toTop) Then
                picture_pushFront(pic, i)
            End If
        Next

        picture_pushBack(pic)

    End Sub

    Private Sub picture_pushFront(ByVal pic As MyPicture, ByVal position As Integer)
        Dim newPictures(pictures.Length) As MyPicture
        For i As Integer = 0 To position - 1
            newPictures(i) = pictures(i)
        Next
        newPictures(position) = pic
        For i As Integer = position + 1 To pictures.Length
            newPictures(i) = pictures(i)
        Next

        pictures = newPictures
    End Sub

    Private Sub picture_pushBack(ByVal pic As MyPicture)
        If pictures Is Nothing Then
            ReDim pictures(0)
            pictures(0) = pic
            Return
        End If

        ReDim Preserve pictures(pictures.Length)
        pictures(pictures.Length - 1) = pic
    End Sub

    Public Sub relateFilenamesToPictures()

        If pictures Is Nothing OrElse pictures(0) Is Nothing Then
            Return
        End If

        Dim filenameCounter = 1
        Dim startCounter As Integer = 0
        Dim endCounter As Integer = 0

        While startCounter < pictures.Length
            While (endCounter + 1 < pictures.Length) AndAlso (pictures(endCounter + 1).page = pictures(startCounter).page)
                endCounter += 1
            End While
            For i As Integer = endCounter To startCounter Step -1
                pictures(i).fileName = "image" & filenameCounter.ToString
                filenameCounter += 1
            Next
            endCounter += 1
            startCounter = endCounter
        End While

    End Sub

    Public Sub insertFormatByStyle(ByVal style As Word.Style)

        If style.NameLocal = "Überschrift" Then

            ' Überschriften-Format steht immer an der 0 und hat in Word die Bezeichnung: "Überschrift"
            Dim newGlobalFormats(globalFormats.Length) As MyFormat
            newGlobalFormats(0) = New MyFormat(style.Font.Name, style.Font.Size, style.Font.Bold, style.Font.Italic, (style.Font.Underline > 0), EBookGenRibbon.rgbIntToHex(style.Font.Color.GetHashCode))

            For i As Integer = 0 To globalFormats.Length - 1
                If Not (globalFormats(i) Is Nothing) Then
                    newGlobalFormats(i + 1) = globalFormats(i)  ' Rest kopieren
                End If
            Next
            globalFormats = newGlobalFormats
        Else
            Dim num As Integer = globalFormats.Length

            'empty globalFormats-Array
            If num = 1 And globalFormats(0) Is Nothing Then
                num = 0
            End If

            ReDim Preserve globalFormats(num)
            globalFormats(num) = New MyFormat(style.Font.Name, style.Font.Size, style.Font.Bold, style.Font.Italic, (style.Font.Underline > 0), EBookGenRibbon.rgbIntToHex(style.Font.Color.GetHashCode))

        End If
    End Sub

    Public Sub insertFormat(ByVal format As MyFormat)
        ' assuming that this is no format from the GUI

        Dim num As Integer = globalFormats.Length

        'empty globalFormats-Array
        If num = 1 And globalFormats(0) Is Nothing Then
            num = 0
        End If

        ReDim Preserve globalFormats(num)
        globalFormats(num) = format
    End Sub

    Public paragraphs() As MyParagraph 'kann sowohl ein Abschnitt mit Sätzen sein, als auch Sonderzeichen für Absatz o.ä.
    Public globalFormats() As MyFormat 'Hier werden alle Formate gespeichert
    Public pictures() As MyPicture ' Bilder in der Reihenfolge, in der sie im Dokument auftauchen

End Class

Public Class MyParagraph

    Public Sub New()
        empty = False
    End Sub

    Public Sub New(ByVal numberOfItems As Integer)
        Me.New()
        ReDim sentences(numberOfItems - 1)
    End Sub

    Public sentences() As MySentence
    Public Property empty() As Boolean

End Class

Public Class MySentence

    Public Sub New()
        specialFormat = New Dictionary(Of Integer, Integer)
    End Sub

    Public Sub New(ByVal numberOfItems As Integer)
        Me.New()
        ReDim words(numberOfItems - 1)
    End Sub

    Public Sub cleanArray()
        Dim i As Integer
        For i = 0 To words.Length - 1
            If words(i) Is Nothing Then
                Exit For
            End If
        Next
        If i < words.Length Then
            ReDim Preserve words(i - 1)
        End If
    End Sub

    Public words() As String    'Array mit allen Wörtern des Satzes

    'hier werden alle Sonderzeichen gespeichert. Gibt es Sonderzeichen, dann ist im words-Array an dieser Stelle ein leerer String ("") und stattdessen ist hier das Sonderzeichen vermerkt.
    'Sonderzeichen sind: <TAB> (Tabulator);
    Public specialChars As New Dictionary(Of Integer, String) ' (Index des Wortes (in words), Sonderzeichen)

    Public Property commonFormat() As Integer 'Index des Feldes in "globalFormats"
    Public specialFormat As Dictionary(Of Integer, Integer) '(Index des Wortes (in words), dazugehöriger Index in "globalFormats")

End Class


Public Class MyFormat

    Public Sub New()
        font = "Arial"
        size = 12
        bold = False
        italic = False
        underlined = False
        colorCode = "000000"
    End Sub

    Public Sub New(ByVal fontName As String, _
            Optional ByVal fontSize As Integer = 12, _
            Optional ByVal bold As Boolean = False, _
            Optional ByVal italic As Boolean = False, _
            Optional ByVal underlined As Boolean = False, _
            Optional ByVal colorCode As String = "000000")

        Me.New()
        Me.font = fontName
        Me.size = fontSize
        Me.bold = bold
        Me.italic = italic
        Me.underlined = underlined
        Me.colorCode = colorCode
    End Sub

    Public Sub New(ByVal word As Word.Range) 'Konstruktor, der ein Format anhand des Formats eines Wortes erstellt

        Me.New()
        Me.font = word.Font.Name
        Me.size = word.Font.Size
        Me.bold = word.Font.Bold
        Me.italic = word.Font.Italic
        Me.underlined = (word.Font.Underline > 0)
        Me.colorCode = EBookGenRibbon.rgbIntToHex(word.Font.Color.GetHashCode)
    End Sub

    Public Function isEqual(ByVal aFormat As MyFormat) As Boolean

        If Me.font <> aFormat.font Then
            Return False
        End If

        If Me.size <> aFormat.size Then
            Return False
        End If

        If Me.bold <> aFormat.bold Then
            Return False
        End If

        If Me.italic <> aFormat.italic Then
            Return False
        End If

        If Me.underlined <> aFormat.underlined Then
            Return False
        End If

        If Me.colorCode <> aFormat.colorCode Then
            Return False
        End If

        Return True
    End Function

    Public Property font() As String
    Public Property size() As Integer
    Public Property bold() As Boolean
    Public Property italic() As Boolean
    Public Property underlined() As Boolean
    Public Property colorCode() As String
    Public Property formatName() As String

End Class

Public Class MyPicture

    Public Sub New(ByVal _height As Decimal, ByVal _width As Decimal, ByVal _toLeft As Decimal, ByVal _toTop As Decimal, ByVal _page As Integer)

        ' the factor 0.351 is to convert from pica-point to mm.
        height = _height * 0.351
        width = _width * 0.351
        toLeft = _toLeft * 0.351 + 0
        toTop = _toTop * 0.351 + 0
        page = _page
        fileName = ""
    End Sub

    Public Property height() As Decimal
    Public Property width() As Decimal
    Public Property toLeft() As Decimal
    Public Property toTop() As Decimal
    Public Property page() As Integer
    Public Property fileName() As String

End Class


